package com.cabd.dao.impl;


import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.cabd.dao.UserDao;
import com.cabd.domain.User;
import com.cabd.utils.DataSourceUtils;


public class UserDaoImpl implements UserDao {

	@Override
	public void add(User user) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "insert into users values(?,?)";
		queryRunner.update(sql, user.getId(), user.getPwd());
	}

	@Override
	public User findUserByUserIdAndPassword(User user) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql="select * from users where id=? and pwd=?";
		
		User user_ = runner.query(sql, new BeanHandler<User>(User.class),user.getId(),user.getPwd());
		
		if(user!=null)
			System.out.println(user.getId() + user.getPwd());
		
		return user_;
	}

	@Override
	public User findUserById(String str) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql = "select * from users where id=?";
		
		return runner.query(sql, new BeanHandler<User>(User.class), str);
	}

	
	
}
